package kr.hkit.powerjava.chapter14.wb.pg;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import javax.swing.JButton;
import javax.swing.JTextField;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.BoxLayout;

public class CalInterest extends JFrame implements ActionListener {

	private JPanel contentPane;
	private LabelandTextPanel panelPrincipal;
	private LabelandTextPanel panelInterest;
	private JButton button;
	private JTextField textField;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CalInterest frame = new CalInterest();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public CalInterest() {
		initialize();
	}
	private void initialize() {
		setTitle("이자계산기");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 241, 157);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
		
		panelPrincipal = new LabelandTextPanel("원금을 입력하시오");
		contentPane.add(panelPrincipal);
		
		panelInterest = new LabelandTextPanel("이율을 입력하시오");
		contentPane.add(panelInterest);
		
		button = new JButton("변환");
		button.addActionListener(this);
		contentPane.add(button);
		
		textField = new JTextField();
		contentPane.add(textField);
		textField.setColumns(15);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("변환")) {
			int principal = 0;
			double interest = 0;
			try{
				principal = Integer.parseInt(panelPrincipal.getTextField().trim());
			}catch (NumberFormatException err) {
				JOptionPane.showMessageDialog(this, "숫자만 가능");
				panelPrincipal.setTextField("");
			}
			try{
				interest = Double.parseDouble(panelInterest.getTextField().trim());
			}catch (NumberFormatException err) {
				JOptionPane.showMessageDialog(this, "숫자만 가능");
				panelInterest.setTextField("");
			}
			textField.setText("이자는 연 " + principal * interest / 100 + "만원 입니다.");
		}
	}
}
